package com.zhangyt.service.impl;

import com.zhangyt.config.IotDBSessionConf;
import com.zhangyt.service.IoTDBService;
import jakarta.annotation.Resource;
import org.apache.iotdb.isession.pool.SessionDataSetWrapper;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.pool.SessionPool;
import org.apache.iotdb.tsfile.read.common.Field;
import org.apache.iotdb.tsfile.read.common.RowRecord;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IoTDBServiceImpl implements IoTDBService {

    @Resource
    private IotDBSessionConf conf;

    @Override
    public SessionDataSetWrapper executeQueryStatement(String sql) {
        SessionPool pool = conf.initSessionPool();
        SessionDataSetWrapper sessionDataSetWrapper = null;
        try {
            sessionDataSetWrapper = pool.executeQueryStatement(sql);
        } catch (IoTDBConnectionException e) {
            throw new RuntimeException(e);
        } catch (StatementExecutionException e) {
            throw new RuntimeException(e);
        }
        return sessionDataSetWrapper;
    }

    @Override
    public SessionDataSetWrapper executeRawDataQuery(List<String> path, long startTime, long endTime, long timeOut) {
        SessionPool pool = conf.initSessionPool();
        SessionDataSetWrapper sessionDataSetWrapper = null;
        try {
            sessionDataSetWrapper = pool.executeRawDataQuery(path, startTime, endTime, timeOut);
        } catch (IoTDBConnectionException e) {
            throw new RuntimeException(e);
        } catch (StatementExecutionException e) {
            throw new RuntimeException(e);
        }

        return sessionDataSetWrapper;
    }


    @Override
    public void printQueryResults(SessionDataSetWrapper sessionDataSetWrapper)  {
        try {
            // 获取列名（即时间序列路径和时间戳）
            List<String> columnNames = sessionDataSetWrapper.getColumnNames();
            System.out.println("Columns: " + String.join(", ", columnNames));

            // 遍历结果集
            while (sessionDataSetWrapper.hasNext()) {
                RowRecord rowRecord = sessionDataSetWrapper.next();
                // 获取时间戳
                long timestamp = rowRecord.getTimestamp();
                System.out.print("Timestamp: " + timestamp + " | ");

                // 获取字段值
                List<Field> fields = rowRecord.getFields();
                for (int i = 0; i < fields.size(); i++) {
                    Field field = fields.get(i);
                    String columnName = columnNames.get(i + 1); // 第一列是时间戳，所以从第二列开始
                    if (field.getDataType() == null) {
                        System.out.print(columnName + ": NULL ");
                    } else {
                        System.out.print(columnName + ": " + field.getStringValue() + " ");
                    }
                }
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭结果集
            sessionDataSetWrapper.close();
        }
    }
}
